package net.ibizsys.eclipse.studio.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * 字符串帮助类
 * 
 * @author <a href='http://www.weibo.com/xjtoo' target='_blank'>Enmaai</a>
 */
public final class StringKit
{
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static char[] chars = "abcdefghigklmnopqrstuvwxyz123456789".toCharArray();

	/**
	 * 生成随机验证码
	 * 
	 * @param length 长度
	 * @return
	 */
	public static String random(int length)
	{
		Random rand = new Random();
		StringBuffer text = new StringBuffer();
		for (int i = 0; i < length; i++)
		{
			text.append(chars[rand.nextInt(chars.length)]);
		}

		return text.toString();
	}

	public static int getInt(Object value, int def)
	{
		try
		{
			if (value instanceof Integer)
			{
				return (Integer) value;
			} else
			{
				return Integer.parseInt(value.toString());
			}

		} catch (Exception ex)
		{
			return def;
		}
	}

	public static long getLong(Object value, long def)
	{
		try
		{
			if (value instanceof Long)
			{
				return (Long) value;
			} else
			{
				return Long.parseLong(value.toString());
			}

		} catch (Exception ex)
		{
			return def;
		}
	}

	/**
	 * 字符串是否相同
	 * 
	 * @param s1
	 * @param s2
	 * @param ignoreCase 是否忽略大小写
	 * @return
	 */
	public static boolean IsEqual(String s1, String s2, boolean ignoreCase)
	{
		if (IsBlank(s1) || IsBlank(s2)) return false;

		if (ignoreCase)
		{
			s1 = s1.toLowerCase();
			s2 = s2.toLowerCase();
		}

		return s1.equals(s2);
	}

	/**
	 * 首字母变小写
	 */
	public static String FirstCharToLowerCase(String str)
	{
		Character firstChar = str.charAt(0);
		String tail = str.substring(1);
		str = Character.toLowerCase(firstChar) + tail;
		return str;
	}

	/**
	 * 首字母变大写
	 */
	public static String FirstCharToUpperCase(String str)
	{
		Character firstChar = str.charAt(0);
		String tail = str.substring(1);
		str = Character.toUpperCase(firstChar) + tail;
		return str;
	}

	/**
	 * 字符串为 null 或者为 "" 时返回 true
	 */
	public static boolean IsBlank(String str)
	{
		return str == null || "".equals(str.trim()) ? true : false;
	}

	/**
	 * 字符串为 null 或者为 "" 时返回 true
	 */
	public static boolean IsBlank(Object str)
	{
		return str == null || "".equals(str) ? true : false;
	}

	/**
	 * 字符串不为 null 而且不为 "" 时返回 true
	 */
	public static boolean NotBlank(String str)
	{
		return str == null || "".equals(str.trim()) ? false : true;
	}

	public static boolean NotBlank(String... strings)
	{
		if (strings == null) return false;
		for (String str : strings)
			if (str == null || "".equals(str.trim())) return false;
		return true;
	}

	/**
	 * 产生随机字符串
	 * 
	 * @return
	 */
	public static String GetRandomString(int length)
	{
		StringBuffer generateRandStr = new StringBuffer();
		Random rand = new Random();
		char ch;
		for (int i = 0; i < length; i++)
		{
			int randNum = Math.abs(rand.nextInt()) % 26 + 97;
			ch = (char) randNum;
			String str = String.valueOf(ch);
			str = Math.abs(rand.nextInt()) % 50 > 25 ? str.toUpperCase() : str;
			generateRandStr.append(str);
		}
		return generateRandStr.toString();
	}

	/**
	 * 产生随机数
	 * 
	 * @return
	 */
	public static String GetRandom(int length)
	{
		StringBuffer generateRandStr = new StringBuffer();
		Random rand = new Random();
		for (int i = 0; i < length; i++)
		{
			int randNum = rand.nextInt(10);

			generateRandStr.append(randNum + "");
		}
		return generateRandStr.toString();
	}

	/**
	 * 获取主键
	 * 
	 * @return
	 */
	public static String GetRandomId()
	{
		return UUID.randomUUID().toString().toUpperCase();
	}

	/**
	 * 获取无'-'的UUID
	 * 
	 * @return
	 */
	public static String Get32RandomId()
	{
		return GetRandomId().replaceAll("-", "");
	}

	/**
	 * 获取日期字符串
	 * 
	 * @return
	 */
	public static String GetDateString()
	{
		return sdf.format(new Date());
	}

	/**
	 * 获取到1970的时间戳（毫秒）
	 * 
	 * @return
	 */
	public static long GetTimestamp()
	{
		Date d = new Date();

		return d.getTime();
	}

	public static int ToInt(String v, int nDef)
	{
		try
		{
			return Integer.parseInt(v);
		} catch (Exception ex)
		{
		}
		return nDef;
	}

	public static int indexOf(String str, String match)
	{
		return str.toUpperCase().indexOf(match.toUpperCase());
	}

	public static boolean has(String str, String match)
	{
		return indexOf(str, match) != -1;
	}
}
